package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.enums.BasicErrorCodeEnum;
import com.fqgj.xjd.user.client.UserActivityService;
import com.fqgj.xjd.user.client.response.UserActivity;
import com.fqgj.xjd.user.dao.UserActivityDao;
import com.fqgj.xjd.user.dao.UserActivityReadDao;
import com.fqgj.xjd.user.entity.UserActivityEntity;
import com.fqgj.xjd.user.entity.UserActivityReadEntity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @program: cashmama-user
 * @description
 * @author: frank
 * @create: 2020-04-16 14:14
 **/
@Service("userActivityService")
public class UserActivityServiceImpl implements UserActivityService {

    @Autowired
    private UserActivityDao userActivityDao;
    @Autowired
    private UserActivityReadDao userActivityReadDao;

    @Override
    public Response<Boolean> readActivity(Long id, String userCode) {
        if (StringUtils.isEmpty(userCode) || id == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param invalid");
        }
        UserActivityEntity userActivity = userActivityDao.selectByPrimaryKey(id);
        if (userActivity == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "activity is not existed");
        }
        UserActivityReadEntity activityRead = userActivityReadDao.getActivityReadByUserCodeAndActivityId(id, userCode, new Date());
        if (activityRead != null) {
            return Response.ok().putData(true);
        }
        UserActivityReadEntity userActivityReadEntity = new UserActivityReadEntity();
        userActivityReadEntity.setActivityId(id);
        userActivityReadEntity.setUserCode(userCode);
        userActivityReadEntity.setType(userActivity.getType());
        userActivityReadEntity.setRead(1);
        userActivityReadEntity.setCreateDate(new Date());
        userActivityReadDao.insert(userActivityReadEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<List<UserActivity>> queryActivityByUserCode(String userCode, Integer type) {
        if (StringUtils.isEmpty(userCode) || type == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param invalid");
        }
        List<UserActivityEntity> userActivityEntities = userActivityDao.selectByParams(Maps.newHashMap());
        userActivityEntities = Optional.ofNullable(userActivityEntities).orElseGet(() -> {
            return Lists.newArrayList();
        }).stream().filter(s -> DateUtil.diffDateToSecond(new Date(), s.getStartDate()) >= 0
                && DateUtil.diffDateToSecond(new Date(), s.getEndDate()) <= 0
                && s.getIsSelling() == 1).collect(Collectors.toList());

        List<UserActivity> userActivities = Lists.newArrayList();
        for (UserActivityEntity entity : userActivityEntities) {
            if (entity.getIntervalType() == 1) {
                if (null != userActivityReadDao.getActivityReadByUserCodeAndActivityId(entity.getId(), userCode, new Date())) {
                    continue;
                }
            }
            UserActivity userActivity = new UserActivity();
            BeanUtils.copyProperties(entity, userActivity);
            userActivity.setActivityId(entity.getId());
            userActivities.add(userActivity);
        }
        return Response.ok().putData(userActivities);
    }

    /**
     * 获取活动/弹窗列表
     *
     * @param packageName
     * @param userCode
     * @param type
     * @return
     */
    @Override
    public Response<List<UserActivity>> queryActivityByUserCode(String packageName, String userCode, Integer type) {
        if (StringUtils.isEmpty(userCode) || type == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param invalid");
        }
        Date now = new Date();
        List<UserActivityEntity> userActivityEntities = userActivityDao.selectUserActivityByPackageName(packageName);
        userActivityEntities = Optional.ofNullable(userActivityEntities).orElseGet(Lists::newArrayList)
                .stream().filter(s -> DateUtil.diffDateToSecond(now, s.getStartDate()) >= 0
                        && DateUtil.diffDateToSecond(now, s.getEndDate()) <= 0
                        && s.getIsSelling() == 1).collect(Collectors.toList());

        List<UserActivity> userActivities = Lists.newArrayList();
        for (UserActivityEntity entity : userActivityEntities) {
            if (entity.getIntervalType() == 1) {
                UserActivityReadEntity activityRead = userActivityReadDao.getActivityReadByUserCodeAndActivityId(entity.getId(), userCode, now);
                if (activityRead != null) {
                    continue;
                }
            }
            UserActivity userActivity = new UserActivity();
            BeanUtils.copyProperties(entity, userActivity);
            userActivity.setActivityId(entity.getId());
            userActivities.add(userActivity);
        }
        return Response.ok().putData(userActivities);
    }
}
